﻿using System;
using System.Xml;
using SS.Ynote.Classic.Properties;

namespace SS.Ynote.Classic
{
    /// <summary>
    /// YnoteSettingsBase Reader
    /// </summary>
    static class SettingsReader
    {
        static public void LoadAllSetting()
        {
            doc = new XmlDocument();
            doc.Load(YnoteSettingsBase.SettingsFile);
            YnoteSettingsBase.HTMLExtensions = new System.Collections.Generic.List<string>();
            YnoteSettingsBase.ActionscriptExtensions = new System.Collections.Generic.List<string>();
            YnoteSettingsBase.AssemblyExtensions = new System.Collections.Generic.List<string>();
            YnoteSettingsBase.BatchExtensions = new System.Collections.Generic.List<string>();
            YnoteSettingsBase.CPPExtensions = new System.Collections.Generic.List<string>();
            YnoteSettingsBase.CSExtensions = new System.Collections.Generic.List<string>();
            YnoteSettingsBase.CSSExtensions = new System.Collections.Generic.List<string>();
            YnoteSettingsBase.DExtensions = new System.Collections.Generic.List<string>();
            YnoteSettingsBase.FSharpExtensions = new System.Collections.Generic.List<string>();
            YnoteSettingsBase.OpenCLExtensions = new System.Collections.Generic.List<string>();
            YnoteSettingsBase.JavaExtensions = new System.Collections.Generic.List<string>();
            YnoteSettingsBase.INIExtensions = new System.Collections.Generic.List<string>();
            YnoteSettingsBase.JScriptExtensions = new System.Collections.Generic.List<string>();
            YnoteSettingsBase.JSONExtension = new System.Collections.Generic.List<string>();
            YnoteSettingsBase.LispExtensions = new System.Collections.Generic.List<string>();
            YnoteSettingsBase.LuaExtensions = new System.Collections.Generic.List<string>();
            YnoteSettingsBase.PascalExtensions = new System.Collections.Generic.List<string>();
            YnoteSettingsBase.PerlExtensions = new System.Collections.Generic.List<string>();
            YnoteSettingsBase.PythonExtensions = new System.Collections.Generic.List<string>();
            YnoteSettingsBase.PHPExtensions = new System.Collections.Generic.List<string>();
            YnoteSettingsBase.RubyExtension = new System.Collections.Generic.List<string>();
            YnoteSettingsBase.QBasicExtensions = new System.Collections.Generic.List<string>();
            YnoteSettingsBase.ScalaExtensions = new System.Collections.Generic.List<string>();
            YnoteSettingsBase.SQLExtensions = new System.Collections.Generic.List<string>();
            YnoteSettingsBase.VBExtensions = new System.Collections.Generic.List<string>();
            YnoteSettingsBase.XMLExtensions = new System.Collections.Generic.List<string>();
            YnoteSettingsBase.CExtensions = new System.Collections.Generic.List<string>();
            YnoteSettingsBase.ASPExtensions = new System.Collections.Generic.List<string>();
            ReadExtensions();
            ReadPreferences();
        }
        /// <summary>
        /// The Xml Document
        /// </summary>
        static XmlDocument doc { get; set; }
        /// <summary>
        /// Reads Extensions
        /// </summary>
        static void ReadExtensions()
        {
            foreach (XmlNode item in doc.SelectNodes("/YnoteSettings/Extensions/Actionscript"))
                YnoteSettingsBase.ActionscriptExtensions.Add(item.InnerText);
            foreach (XmlNode item in doc.SelectNodes("/YnoteSettings/Extensions/HTML"))
                YnoteSettingsBase.HTMLExtensions.Add(item.InnerText);
            foreach (XmlNode item in doc.SelectNodes("/YnoteSettings/Extensions/Batch"))
                YnoteSettingsBase.BatchExtensions.Add(item.InnerText);
            foreach (XmlNode item in doc.SelectNodes("/YnoteSettings/Extensions/CPP"))
                YnoteSettingsBase.CPPExtensions.Add(item.InnerText);
            foreach (XmlNode item in doc.SelectNodes("/YnoteSettings/Extensions/CSS"))
                YnoteSettingsBase.CSSExtensions.Add(item.InnerText);
            foreach (XmlNode item in doc.SelectNodes("/YnoteSettings/Extensions/CSharp"))
                YnoteSettingsBase.CSExtensions.Add(item.InnerText);
            foreach (XmlNode item in doc.SelectNodes("/YnoteSettings/Extensions/Ini"))
                YnoteSettingsBase.INIExtensions.Add(item.InnerText);
            foreach (XmlNode item in doc.SelectNodes("/YnoteSettings/Extensions/Java"))
                YnoteSettingsBase.JavaExtensions.Add(item.InnerText);
            foreach (XmlNode item in doc.SelectNodes("/YnoteSettings/Extensions/JS"))
                YnoteSettingsBase.JScriptExtensions.Add(item.InnerText);
            foreach (XmlNode item in doc.SelectNodes("/YnoteSettings/Extensions/JSON"))
                YnoteSettingsBase.JSONExtension.Add(item.InnerText);
            foreach (XmlNode item in doc.SelectNodes("/YnoteSettings/Extensions/Lua"))
                YnoteSettingsBase.LuaExtensions.Add(item.InnerText);
            foreach (XmlNode item in doc.SelectNodes("/YnoteSettings/Extensions/PHP"))
                YnoteSettingsBase.PHPExtensions.Add(item.InnerText);
            foreach (XmlNode item in doc.SelectNodes("/YnoteSettings/Extensions/Python"))
                YnoteSettingsBase.PythonExtensions.Add(item.InnerText);
            foreach (XmlNode item in doc.SelectNodes("/YnoteSettings/Extensions/Ruby"))
                YnoteSettingsBase.RubyExtension.Add(item.InnerText);
            foreach (XmlNode item in doc.SelectNodes("/YnoteSettings/Extensions/SQL"))
                YnoteSettingsBase.SQLExtensions.Add(item.InnerText);
            foreach (XmlNode item in doc.SelectNodes("/YnoteSettings/Extensions/VB"))
                YnoteSettingsBase.VBExtensions.Add(item.InnerText);
            foreach (XmlNode item in doc.SelectNodes("/YnoteSettings/Extensions/Lisp"))
                YnoteSettingsBase.LispExtensions.Add(item.InnerText);
            foreach (XmlNode item in doc.SelectNodes("/YnoteSettings/Extensions/OpenCL"))
                YnoteSettingsBase.OpenCLExtensions.Add(item.InnerText);
            foreach (XmlNode item in doc.SelectNodes("/YnoteSettings/Extensions/FSharp"))
                YnoteSettingsBase.FSharpExtensions.Add(item.InnerText);
            foreach (XmlNode item in doc.SelectNodes("/YnoteSettings/Extensions/Pascal"))
                YnoteSettingsBase.PascalExtensions.Add(item.InnerText);
            foreach (XmlNode item in doc.SelectNodes("/YnoteSettings/Extensions/Perl"))
                YnoteSettingsBase.PerlExtensions.Add(item.InnerText);
            foreach (XmlNode item in doc.SelectNodes("/YnoteSettings/Extensions/Assembly"))
                YnoteSettingsBase.AssemblyExtensions.Add(item.InnerText);
            foreach (XmlNode item in doc.SelectNodes("/YnoteSettings/Extensions/Scala"))
                YnoteSettingsBase.ScalaExtensions.Add(item.InnerText);
            foreach (XmlNode item in doc.SelectNodes("/YnoteSettings/Extensions/QBasic"))
                YnoteSettingsBase.QBasicExtensions.Add(item.InnerText);
            foreach (XmlNode item in doc.SelectNodes("/YnoteSettings/Extensions/D"))
                YnoteSettingsBase.DExtensions.Add(item.InnerText);
            foreach (XmlNode item in doc.SelectNodes("/YnoteSettings/Extensions/ASP"))
                YnoteSettingsBase.ASPExtensions.Add(item.InnerText);
            foreach (XmlNode item in doc.SelectNodes("/YnoteSettings/Extensions/C"))
                YnoteSettingsBase.CExtensions.Add(item.InnerText);
            foreach (XmlNode item in doc.SelectNodes("/YnoteSettings/Extensions/Xml"))
                YnoteSettingsBase.XMLExtensions.Add(item.InnerText);
        }
        static void ReadPreferences()
        {
            foreach (XmlNode item in doc.SelectNodes("/YnoteSettings/TextAreaType"))
                YnoteSettingsBase.TextAreaType = (FastColoredTextBoxNS.TextAreaBorderType)Enum.Parse(typeof(FastColoredTextBoxNS.TextAreaBorderType), item.InnerText);
            foreach (XmlNode item in doc.SelectNodes("/YnoteSettings/ShowCaret"))
                YnoteSettingsBase.ShowCaret = item.InnerText.ToBool();
            foreach (XmlNode item in doc.SelectNodes("/YnoteSettings/ShowFoldingLines"))
                YnoteSettingsBase.ShowFoldingLines = item.InnerText.ToBool();
            foreach (XmlNode item in doc.SelectNodes("/YnoteSettings/ShowLineNumbers"))
                YnoteSettingsBase.ShowLineNumbers = item.InnerText.ToBool();
            foreach (XmlNode item in doc.SelectNodes("/YnoteSettings/WordWrapMode"))
                YnoteSettingsBase.WordWrapMode = (FastColoredTextBoxNS.WordWrapMode)Enum.Parse(typeof(FastColoredTextBoxNS.WordWrapMode), item.InnerText);
            foreach (XmlNode item in doc.SelectNodes("/YnoteSettings/LineInterval"))
                YnoteSettingsBase.LineInterval = item.InnerText.ToInt();
            foreach (XmlNode item in doc.SelectNodes("/YnoteSettings/PaddingWidth"))
                YnoteSettingsBase.PaddingWidth = item.InnerText.ToInt();
            foreach(XmlNode item in doc.SelectNodes("/YnoteSettings/HighlightStrategy"))
                YnoteSettingsBase.HighlightStrategy = (FastColoredTextBoxNS.HighlightingRangeType)Enum.Parse(typeof(FastColoredTextBoxNS.HighlightingRangeType), item.InnerText);
            foreach(XmlNode item in doc.SelectNodes("/YnoteSettings/EnableMouse"))
                YnoteSettingsBase.EnableMouse = item.InnerText.ToBool();
            foreach(XmlNode item in doc.SelectNodes("/YnoteSettings/OpenLastSession"))
                YnoteSettingsBase.OpenLastSession = item.InnerText.ToBool();
            foreach (XmlNode item in doc.SelectNodes("/YnoteSettings/TabLocation"))
                YnoteSettingsBase.TabLocation = (WeifenLuo.WinFormsUI.Docking.DocumentTabStripLocation)Enum.Parse(typeof(WeifenLuo.WinFormsUI.Docking.DocumentTabStripLocation), item.InnerText);
            foreach (XmlNode item in doc.SelectNodes("/YnoteSettings/AlwaysShowRuler"))
                YnoteSettingsBase.AlwaysShowRuler = item.InnerText.ToBool();
        }
    }
    /// <summary>
    /// Extensinos
    /// </summary>
    static class Extensions
    {
        /// <summary>
        /// strint toint
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static int ToInt(this string str)
        {
            int i;
            int.TryParse(str, out i);
            return i;
        }
        /// <summary>
        /// string to bool
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool ToBool(this string str)
        {
            if (str == "true")
                return true;
            else if (str == "false")
                return false;
            return false;
        }
    }
}
